package com.erpanderp.wam.usm.model.service.user;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.usm.model.entity.user.NewJoinee;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class NewJoineeDAO {
	
	public boolean registerNewJoinee(NewJoinee newJoinee){
		System.out.println("IN REGISTER NEWJOINEE METHOD");
		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.save(newJoinee);
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
		
	}
	
	public NewJoinee getNewJoineeById(Long newJoineeId){
		System.out.println("IN REGISTER NEWJOINEE METHOD");
		NewJoinee newJoinee = null;
		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from NewJoinee n where n.newJoineeId=:newJoineeId";
			Query query = session.createQuery(queryString).setLong("newJoineeId", newJoineeId);
			newJoinee = (NewJoinee) query.uniqueResult();
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return newJoinee;
		
	}
	
	public NewJoinee enrollNewJoinee(NewJoinee newJoinee){
		System.out.println("IN REGISTER NEWJOINEE METHOD");
		try{
			
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.saveOrUpdate(newJoinee);
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return newJoinee;
		
	}
	
	public List<NewJoinee> getAllNewJoinees(){
		List<NewJoinee> newJoineesList = new ArrayList<NewJoinee>();
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from NewJoinee";
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			newJoineesList = (List<NewJoinee>)query.list();
			session.getTransaction().commit();
			return newJoineesList;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	public List<NewJoinee> getUnregisteredNewJoinees(){
		List<NewJoinee> newJoineesList = new ArrayList<NewJoinee>();
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.setFlushMode(FlushMode.AUTO);
			session.beginTransaction();
			String queryString = "from NewJoinee n where n.enrolementStatus != null and n.enrolementStatus != :enrollmentStatus";
			Query query = session.createQuery(queryString).setString("enrollmentStatus", CommonLookup.ENROLLMENT_STATUS_ENROLLED);
			query.setCacheable(false);
			newJoineesList = (List<NewJoinee>)query.list();
			System.out.println("NEW JOINEES LIST SIZE ------------+"+newJoineesList.size());
			session.getTransaction().commit();
			return newJoineesList;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	
	public static void main(String a[]){
		System.out.println(new NewJoineeDAO().getUnregisteredNewJoinees());
	}

}
